<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Simple but Safe Comment Removal</title>
<style>
  body {
    font-size: .8em;
    padding: 10px;
    margin: 0;
    font-family: "Consolas", "Lucida Console", Courier, monospace;
  }

  #a {
    width: 700px;
    height: 250px;
    font-size: 1em;
    padding: 10px;
  }

  #b {
    display: block;
    font-size: 1.4em;
  }

  #c {
    color: white;
    padding: 10px;
    background: #222;
    width: 700px;
  }
</style>
</head>

<body>
<h1>Simple but Safe Comment Removal</h1>

<textarea id="a">
  /**
  * @fileoverview Module authoring format.
  */

  var define = function() {
  // some comment
  var reg = /.*/g; // comment */
  }

  /* ok, I will disappear. */
  var str = '// i am string';

  /* will not // be removed */ var xx = 'a';

  //
  //     var Calendar = require('calendar');
</textarea>
<button id="b">Remove Comments</button>
<pre id="c"></pre>

<script>

  $ = function(id) {
    return document.getElementById(id)
  }

  $('b').onclick = function() {
    $('c').innerHTML = removeCommentsSafely($('a').value).replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/\n/g, '<br/>')
  }

  function removeCommentsSafely(code) {

    // multi-line comments
    code = code.replace(/^\s*\/\*[\s\S]*?\*\/\s*$/mg, '')

    // single-line comments
    code = code.replace(/^\s*\/\/.*$/mg, '')

    return code
  }

</script>


<h3>References</h3>

<ul>
  <li><a href="http://james.padolsey.com/demos/comment-removal-js.html">http://james.padolsey.com/demos/comment-removal-js.html</a></li>
</ul>

</body>
</html>
